<?php

namespace app\modules\clinical\controllers;

use Yii;
use app\modules\clinical\models;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\components\PtAccessControl;

/**
 * CheckController implements the CRUD actions for Check model.
 */
class CheckController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
            ]
        ];
    }

    /**
     * Lists all Check models.
     * @return mixed
     */
    public function actionIndex()
    {
        $pt_id = Yii::$app->session->get('pt_id');
        $dataProvider = new ActiveDataProvider([
            'query' => models\Check::find()->where(['delete_at' => 0])->andWhere(['or', 'pt_id='.$pt_id, 'rel_id='.$pt_id]),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'pt_rel_list' => models\Patient::findOne($pt_id)->getRelations(),
        ]);
    }

    /**
     * Displays a single Check model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
            'pt_rel_list' => models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelations(),
        ]);
    }

    /**
     * Creates a new Check model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new models\Check();
        $model->defaultValue();
        $model->pt_id = Yii::$app->request->post('pt_rel', false);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['/clinical/check/view/', 'id' => $model->pk_id]);
        }

        return $this->render('create', [
            'model' => $model,
            'pt_rel_list' => $this->getRelatives(),
        ]);
    }

    /**
     * Updates an existing Check model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['/clinical/check/view/', 'id' => $id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'pt_rel_list' => $this->getRelatives(),
                'pt_rel' => ($model->rel_id != 0) ? $model->pt_id : 0,
            ]);
        }
    }

    /**
     * Deletes an existing Check model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete_at = strtotime(date("Y-m-d H:i:s"));
        if ($model->save()) {
            return $this->redirect('/clinical/check');
        }

        return $this->redirect('/clinical/check/view?id='.$id);
    }

    /**
     * Finds the Check model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Check the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = models\Check::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    protected function getRelatives()
    {
        $rel = models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelatives();
        return $rel;
    }
}
